package com.wyj.replaceLink;

import java.io.*;

public class Main {
    /*
使用：一般都将.md文件复制粘贴到.txt文件内进行操作，经修改后在复制粘贴到.md文件内
修改前：
1.  点击 启动![image-20200528122641369](D:\ASUS\Desktop\前端基础\javaWeb\javaWeb.assets\image-20200528122641369.png)
3. <img src="D:\ASUS\Desktop\前端基础\javaWeb\javaWeb.assets\image-20200528122133306.png" alt="image-20200528122133306" style="zoom:33%;" />
   <img src="D:\ASUS\Desktop\前端基础\javaWeb\javaWeb.assets\image-20200528094540998.png" alt="image-20200528094540998" style="zoom:33%;" />
修改后：
1.  点击 启动![image-20200528122641369](javaWeb.assets\image-20200528122641369.png)
3. ![image-20200528122133306](javaWeb.assets/image-20200528122133306.png)
   ![image-20200528094540998](javaWeb.assets/image-20200528094540998.png)
     */
    public static void main(String[] args) {
        // 读取文件的地址
        String readPath = "D:\\Users\\Swift\\Desktop\\dome.txt";
        // 写入文件的地址
        String writePath = "D:\\Users\\Swift\\Desktop\\java.txt";
        try {
//            Main.writeFiles(writePath, Main.readFiles(readPath));
            Main.readFiles(readPath);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    // 读取文档内容并进行修改
    public static String readFiles(String readPath)  {
        BufferedReader br = null;
        StringBuffer buffer = new StringBuffer();
        try {
            br = new BufferedReader(new FileReader(readPath));
            buffer = new StringBuffer();
            String line = "";
            while ((line = br.readLine()) != null){
                // 此处根据实际需要修改某些行的内容
                    // 对内容进行判断是否要修改
               if (line.contains("![im") && line.contains("AS")){
                   //1.启动![image-20200528122641369](D:\ASUS\Desktop\前端基础\javaWeb\javaWeb.assets\image-20200528122641369.png)
                    int index = line.indexOf('!');
                    // 0--index：1.启动
                    // line.substring(index).substring(0, 27)：![image-20200528122641369](
                    // line.substring(index).substring(56)：javaWeb.assets\image-20200528122641369.png)
                    buffer.append(line.substring(0, index) + line.substring(index).substring(0, 27) + line.substring(index).substring(56));
//                    System.out.println(line.substring(0, index) + line.substring(index).substring(0, 27) + line.substring(index).substring(56));
                }else if (line.contains("<img src=") && line.contains("AS")){
                   //1.启动<img src="D:\ASUS\Desktop\前端基础\javaWeb\javaWeb.assets\image-20200528122133306.png" />
                    int index = line.indexOf('<');
                    String[] split = line.substring(index).split("\\\\");
                    // img：image-20200528122133306.png
                    String img = split[6].substring(0, split[6].indexOf("\""));
                   // 第一段 + 第二段：图片
                    buffer.append(line.substring(0, index) + "![" + img.substring(0, img.indexOf('.')) + "](javaWeb.assets/" + img + ")");
//                   System.out.println(line.substring(0, index) + "![" + img.substring(0, img.indexOf('.')) + "](javaWeb.assets/" + img + ")");
               }
               // 如果不用修改, 则按原来的内容回写
                else {
                    buffer.append(line);
                }
                // 添加换行
                buffer.append("\r\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("File read OK!!!");
        }
        return buffer.toString();
    }

    // 将内容写入文档
    public static void writeFiles(String writePath, String content){
        BufferedWriter bw = null;
        try {
            bw = new BufferedWriter(new FileWriter(writePath));
            bw.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                System.out.println("File close OK!!!");
            }
        }

    }
    
}

